<?php

namespace App\Service\OpenShop\Order;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrderTagsMappingModel;
use App\Service\BaseService;
use Illuminate\Support\Facades\DB;

class OrderTagsService extends BaseService
{
    public static function getPrepareTagCount(array $orderPermissionList)
    {
        $orderList = OpenShopMainProcessModel::query()
                                             ->join('open_shop_sign_order as order', 'order.mainProcessId', '=', 'open_shop_main_process.id')
                                             ->where('order.status', SignOrderEnum::ORDER_STATUS_OK)
                                             ->when(!$orderPermissionList['isAll'],
                                                 function ($query) use ($orderPermissionList) {
                                                     $query->whereRaw("open_shop_main_process." . $orderPermissionList['rawSql']);
                                                 })
                                             ->whereIn('nowStepStatus', MissionTemplateEnum::PREPARE_STATUS_NEW)
                                             ->select("order.orderNo")
                                             ->get();

        $tagList = OpenShopOrderTagsMappingModel::query()
                                                ->whereIn('orderNo', $orderList)
                                                ->select("tagId", DB::raw("count(*) as ct"))
                                                ->groupBy('tagId')
                                                ->get()
                                                ->keyBy('tagId')
                                                ->toArray();

        return $tagList;
    }

    /**
     * 带上状态
     *
     * @param array $orderPermissionList
     * @return array
     */
    public static function getPrepareTagCountWithNowStepStatus(array $orderPermissionList, array $nowStepStatus)
    {
        // 需求未确定, 暂时这么写, 等后期需求明确了优化
        $orderList = OpenShopMainProcessModel::query()
                                             ->join('open_shop_sign_order as order', 'order.mainProcessId', '=',
                                                 'open_shop_main_process.id')
                                             ->where('order.status', '!=', SignOrderEnum::ORDER_STATUS_NO)
                                             ->when(!$orderPermissionList['isAll'],
                                                 function ($query) use ($orderPermissionList) {
                                                     $query->whereRaw("open_shop_main_process." . $orderPermissionList['rawSql']);
                                                 })
                                             ->whereIn('nowStepStatus', $nowStepStatus)
                                             ->select("order.orderNo")
                                             ->get();

        $tagList = OpenShopOrderTagsMappingModel::query()
                                                ->whereIn('orderNo', $orderList)
                                                ->select("tagId", DB::raw("count(*) as ct"))
                                                ->groupBy('tagId')
                                                ->get()
                                                ->keyBy('tagId')
                                                ->toArray();

        return $tagList;
    }


    public static function preparationOverview(array $orderPermission)
    {
        // 筹备各个阶段的数据
        $stepCounts = OrderTagsService::getPrepareTagCount($orderPermission);

        $showStepStatusLists = collect($stepCounts)->map(function ($item) {
            return $item['ct'];
        });

        // 匿名方法，获取stepStatus数组的总count
        $countInStepStatus = function (array $stepStatusArr) use ($showStepStatusLists) {
            $count = 0;
            foreach ($showStepStatusLists as $stepStatus => $ct) {
                if (in_array($stepStatus, $stepStatusArr)) {
                    $count += $ct;
                }
            }

            return $count;
        };

        $lists = [
            [
                'text'          => '全部筹备中',
                'lists'         => [
                    [
                        'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[OpenShopOrderTagsMappingModel::TAG_TYPE_1],
                        'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_1,
                        'count'         => $showStepStatusLists[$stepStatus] ?? 0,
                    ],
                    [
                        'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[OpenShopOrderTagsMappingModel::TAG_TYPE_2],
                        'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_2,
                        'count'         => $showStepStatusLists[$stepStatus] ?? 0,
                    ]
                ],
                'stepStatusStr' => implode(',', $stepStatusArr = [
                    OpenShopOrderTagsMappingModel::TAG_TYPE_1,
                    OpenShopOrderTagsMappingModel::TAG_TYPE_2,
                ]),
                'count'         => $countInStepStatus($stepStatusArr),
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_3,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_4,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_5,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_6,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_7,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_8,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_9,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_11,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_19,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_20,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_12,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_13,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_14,
                'text'          => '门店正常',
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],
            [
                'stepStatusStr' => (string)$stepStatus = OpenShopOrderTagsMappingModel::TAG_TYPE_15,
                'text'          => OpenShopOrderTagsMappingModel::TAG_TYPE_MAP_TEXT[$stepStatus],
                'lists'         => [],
                'count'         => $showStepStatusLists[$stepStatus] ?? 0,
            ],

        ];


        return $lists;
    }

}
